package com.openbravo.pos.sql;

import com.openbravo.basic.BasicException;
import com.openbravo.data.loader.BaseSentence;
import com.openbravo.data.loader.BatchSentenceScript;
import com.openbravo.data.loader.DataResultSet;
import com.openbravo.data.loader.Datas;
import com.openbravo.data.loader.MetaSentence;
import com.openbravo.data.loader.SerializerReadBasic;
import com.openbravo.data.loader.SerializerWriteBasic;
import com.openbravo.data.loader.Session;
import com.openbravo.data.loader.StaticSentence;
import com.openbravo.pos.forms.AppView;
import com.openbravo.pos.forms.JPanelView;
import com.openbravo.pos.panels.SQLDatabase;
import com.openbravo.pos.panels.SQLTable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PrintStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTree;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeExpansionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

public class JPanelSQL extends JPanel
  implements JPanelView
{
  private ArrayList m_aHistory;
  private int m_iHistoryIndex;
  private int m_iResultIndex;
  private AppView m_App;
  private JButton jButton1;
  private JButton jButton2;
  private JScrollPane jScrollPane1;
  private JScrollPane jScrollPane4;
  private JTabbedPane m_TabbedPane;
  private JButton m_jBack;
  private JButton m_jForward;
  private JTree m_jTreeDB;
  private JButton m_jbegintrans;
  private JButton m_jcommit;
  private JButton m_jrollback;
  private JButton m_jtabdelete;
  private JTextArea m_jtxtSQL;

  public JPanelSQL(AppView paramAppView)
  {
    this.m_App = paramAppView;
    this.m_aHistory = new ArrayList();
    this.m_aHistory.add("");
    this.m_iHistoryIndex = 0;
    initComponents();
    this.m_iResultIndex = 0;
    addResultTab();
    DefaultMutableTreeNode localDefaultMutableTreeNode = new DefaultMutableTreeNode("Openbravo POS");
    SQLDatabase localSQLDatabase = new SQLDatabase("Openbravo POS - Database.");
    try
    {
      MetaSentence localMetaSentence = new MetaSentence(this.m_App.getSession(), "getTables", new SerializerWriteBasic(new Datas[] { Datas.STRING, Datas.STRING }), new SerializerReadBasic(new Datas[] { Datas.STRING, Datas.STRING, Datas.STRING, Datas.STRING, Datas.STRING }));
      DataResultSet localDataResultSet = localMetaSentence.openExec(new Object[] { null, null });
      Object[] arrayOfObject;
      while (localDataResultSet.next())
      {
        arrayOfObject = (Object[])(Object[])localDataResultSet.getCurrent();
        localSQLDatabase.addTable((String)arrayOfObject[2]);
      }
      localDataResultSet.close();
      localMetaSentence.closeExec();
      localMetaSentence = new MetaSentence(this.m_App.getSession(), "getColumns", new SerializerWriteBasic(new Datas[] { Datas.STRING, Datas.STRING, Datas.STRING }), new SerializerReadBasic(new Datas[] { Datas.STRING, Datas.STRING, Datas.STRING, Datas.STRING, Datas.STRING }));
      localDataResultSet = localMetaSentence.openExec(new Object[] { null, null, null });
      while (localDataResultSet.next())
      {
        arrayOfObject = (Object[])(Object[])localDataResultSet.getCurrent();
        SQLTable localSQLTable = localSQLDatabase.getTable((String)arrayOfObject[2]);
        if (localSQLTable != null)
          localSQLTable.addColumn((String)arrayOfObject[3]);
      }
      localDataResultSet.close();
      localMetaSentence.closeExec();
    }
    catch (BasicException localBasicException)
    {
    }
    DefaultTreeModel localDefaultTreeModel = new DefaultTreeModel(localSQLDatabase);
    this.m_jTreeDB.setModel(localDefaultTreeModel);
    statusButtons();
  }

  public JComponent getComponent()
  {
    return this;
  }

  public String getTitle()
  {
    return null;
  }

  public void activate()
    throws BasicException
  {
  }

  public boolean deactivate()
  {
    return true;
  }

  private void addResultTab()
  {
    this.m_iResultIndex += 1;
    this.m_TabbedPane.addTab("Resultset " + Integer.toString(this.m_iResultIndex), new JPanelSQLResult());
    this.m_TabbedPane.setSelectedIndex(this.m_TabbedPane.getTabCount() - 1);
    this.m_jtabdelete.setEnabled(this.m_TabbedPane.getTabCount() > 1);
  }

  private void statusButtons()
  {
    this.m_jForward.setEnabled(this.m_iHistoryIndex < this.m_aHistory.size() - 1);
    this.m_jBack.setEnabled(this.m_iHistoryIndex > 0);
  }

  private boolean executeSentence(BaseSentence paramBaseSentence)
  {
    return executeSentence(paramBaseSentence, null);
  }

  private boolean executeSentence(BaseSentence paramBaseSentence, Object paramObject)
  {
    JPanelSQLResult localJPanelSQLResult = (JPanelSQLResult)this.m_TabbedPane.getSelectedComponent();
    return localJPanelSQLResult.executeSentence(paramBaseSentence, paramObject);
  }

  private void insertProductCategories()
  {
    try
    {
      for (int i = 100; i < 50000; i++)
      {
        String str = "INSERT INTO m_product_category( m_product_category_id, ad_client_id, ad_org_id, isactive,  created, createdby,  updated, updatedby,  value,  name,  description,  isdefault, plannedmargin, a_asset_group_id) VALUES (" + Integer.toString(i + 1000000) + " ," + " 1000000, " + " 0, " + "'Y', " + " '2007-01-01', 100, " + " '2007-01-01', 100, " + " 'Fictious Category no " + Integer.toString(i) + "' ," + " 'Fictious Category Name no " + Integer.toString(i) + "' ," + " null," + " 'N'," + " 0," + " null)";
        System.out.println(i);
        System.out.println(str);
        StaticSentence localStaticSentence = new StaticSentence(this.m_App.getSession(), str);
        localStaticSentence.exec();
      }
    }
    catch (BasicException localBasicException)
    {
      localBasicException.printStackTrace();
    }
  }

  private void initComponents()
  {
    this.jButton1 = new JButton();
    this.jScrollPane1 = new JScrollPane();
    this.m_jtxtSQL = new JTextArea();
    this.jScrollPane4 = new JScrollPane();
    this.m_jTreeDB = new JTree();
    this.m_jForward = new JButton();
    this.m_jBack = new JButton();
    this.m_TabbedPane = new JTabbedPane();
    this.jButton2 = new JButton();
    this.m_jtabdelete = new JButton();
    this.m_jbegintrans = new JButton();
    this.m_jcommit = new JButton();
    this.m_jrollback = new JButton();
    setLayout(null);
    this.jButton1.setIcon(new ImageIcon(getClass().getResource("/com/openbravo/images/launch.png")));
    this.jButton1.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent paramActionEvent)
      {
        JPanelSQL.this.jButton1ActionPerformed(paramActionEvent);
      }
    });
    add(this.jButton1);
    this.jButton1.setBounds(460, 10, 60, 80);
    this.jScrollPane1.setViewportView(this.m_jtxtSQL);
    add(this.jScrollPane1);
    this.jScrollPane1.setBounds(10, 40, 440, 50);
    this.m_jTreeDB.setRootVisible(false);
    this.m_jTreeDB.addTreeExpansionListener(new TreeExpansionListener()
    {
      public void treeCollapsed(TreeExpansionEvent paramTreeExpansionEvent)
      {
      }

      public void treeExpanded(TreeExpansionEvent paramTreeExpansionEvent)
      {
        JPanelSQL.this.m_jTreeDBTreeExpanded(paramTreeExpansionEvent);
      }
    });
    this.jScrollPane4.setViewportView(this.m_jTreeDB);
    add(this.jScrollPane4);
    this.jScrollPane4.setBounds(530, 10, 150, 320);
    this.m_jForward.setIcon(new ImageIcon(getClass().getResource("/com/openbravo/images/1rightarrow.png")));
    this.m_jForward.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent paramActionEvent)
      {
        JPanelSQL.this.m_jForwardActionPerformed(paramActionEvent);
      }
    });
    add(this.m_jForward);
    this.m_jForward.setBounds(70, 10, 49, 25);
    this.m_jBack.setIcon(new ImageIcon(getClass().getResource("/com/openbravo/images/1leftarrow.png")));
    this.m_jBack.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent paramActionEvent)
      {
        JPanelSQL.this.m_jBackActionPerformed(paramActionEvent);
      }
    });
    add(this.m_jBack);
    this.m_jBack.setBounds(10, 10, 50, 25);
    add(this.m_TabbedPane);
    this.m_TabbedPane.setBounds(10, 130, 510, 200);
    this.jButton2.setIcon(new ImageIcon(getClass().getResource("/com/openbravo/images/editnew.png")));
    this.jButton2.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent paramActionEvent)
      {
        JPanelSQL.this.jButton2ActionPerformed(paramActionEvent);
      }
    });
    add(this.jButton2);
    this.jButton2.setBounds(10, 100, 49, 25);
    this.m_jtabdelete.setIcon(new ImageIcon(getClass().getResource("/com/openbravo/images/editdelete.png")));
    this.m_jtabdelete.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent paramActionEvent)
      {
        JPanelSQL.this.m_jtabdeleteActionPerformed(paramActionEvent);
      }
    });
    add(this.m_jtabdelete);
    this.m_jtabdelete.setBounds(70, 100, 49, 25);
    this.m_jbegintrans.setText("trans");
    this.m_jbegintrans.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent paramActionEvent)
      {
        JPanelSQL.this.m_jbegintransActionPerformed(paramActionEvent);
      }
    });
    add(this.m_jbegintrans);
    this.m_jbegintrans.setBounds(210, 10, 57, 23);
    this.m_jcommit.setText("commit");
    this.m_jcommit.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent paramActionEvent)
      {
        JPanelSQL.this.m_jcommitActionPerformed(paramActionEvent);
      }
    });
    add(this.m_jcommit);
    this.m_jcommit.setBounds(300, 10, 65, 23);
    this.m_jrollback.setText("rollback");
    this.m_jrollback.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent paramActionEvent)
      {
        JPanelSQL.this.m_jrollbackActionPerformed(paramActionEvent);
      }
    });
    add(this.m_jrollback);
    this.m_jrollback.setBounds(370, 10, 69, 23);
  }

  private void m_jbegintransActionPerformed(ActionEvent paramActionEvent)
  {
    try
    {
      this.m_App.getSession().begin();
    }
    catch (SQLException localSQLException)
    {
      System.out.println(localSQLException);
    }
  }

  private void m_jrollbackActionPerformed(ActionEvent paramActionEvent)
  {
    try
    {
      this.m_App.getSession().rollback();
    }
    catch (SQLException localSQLException)
    {
      System.out.println(localSQLException);
    }
  }

  private void m_jcommitActionPerformed(ActionEvent paramActionEvent)
  {
    try
    {
      this.m_App.getSession().commit();
    }
    catch (SQLException localSQLException)
    {
      System.out.println(localSQLException);
    }
  }

  private void m_jtabdeleteActionPerformed(ActionEvent paramActionEvent)
  {
    this.m_TabbedPane.removeTabAt(this.m_TabbedPane.getSelectedIndex());
    this.m_jtabdelete.setEnabled(this.m_TabbedPane.getTabCount() > 1);
  }

  private void jButton2ActionPerformed(ActionEvent paramActionEvent)
  {
    addResultTab();
  }

  private void m_jForwardActionPerformed(ActionEvent paramActionEvent)
  {
    this.m_iHistoryIndex += 1;
    this.m_jtxtSQL.setText((String)this.m_aHistory.get(this.m_iHistoryIndex));
    statusButtons();
  }

  private void m_jBackActionPerformed(ActionEvent paramActionEvent)
  {
    this.m_iHistoryIndex -= 1;
    this.m_jtxtSQL.setText((String)this.m_aHistory.get(this.m_iHistoryIndex));
    statusButtons();
  }

  private void m_jTreeDBTreeExpanded(TreeExpansionEvent paramTreeExpansionEvent)
  {
  }

  private void jButton1ActionPerformed(ActionEvent paramActionEvent)
  {
    BatchSentenceScript localBatchSentenceScript = new BatchSentenceScript(this.m_App.getSession(), this.m_jtxtSQL.getText());
    if ((executeSentence(localBatchSentenceScript)) && (!this.m_aHistory.get(this.m_iHistoryIndex).equals(this.m_jtxtSQL.getText())))
    {
      this.m_iHistoryIndex += 1;
      this.m_aHistory.subList(this.m_iHistoryIndex, this.m_aHistory.size()).clear();
      this.m_aHistory.add(this.m_jtxtSQL.getText());
      statusButtons();
    }
  }
}

/* Location:           D:\PowerFulPOS-2.30.2\openbravopos.jar
 * Qualified Name:     com.openbravo.pos.sql.JPanelSQL
 * JD-Core Version:    0.6.0
 */